package com.neusoft.neu24.utils;

import com.neusoft.neu24.entity.Grid;

import java.util.ArrayList;
import java.util.List;

public class fillGrid {
    public static Grid list2Tree(List<Grid> grids) {
//        1、寻找根节点
        Grid root = null;
        for (Grid grid : grids) {
            if (grid.getPid() == 0) {
                root = grid;
                break;
            }
        }

//        填充根节点的孩子
        fillChildren(root, grids);

        return root;

    }

    private static void fillChildren(Grid root, List<Grid> grids) {
        if (root == null || grids == null || grids.size() == 0) {
            return;
        }
        if (root.getChildren() == null) {
            root.setChildren(new ArrayList<Grid>());
        }

        for (Grid grid : grids) {
            if (grid.getPid() != null && grid.getPid().intValue() == root.getId().intValue()) {
                root.getChildren().add(grid);
            }
        }
//        递归填充孩子节点
        if (root.getChildren() == null || root.getChildren().size() == 0) {
            return;
        }

        for (Grid grid : root.getChildren()) {
            fillChildren(grid, grids);
        }
    }

    public static List<Grid> fillContent(List<Grid> grids) {
        List<Grid> newGrids = new ArrayList<>();
        for (Grid grid : grids) {
            List<Grid> son = grid.getChildren();
            String pname = grid.getGname();
            grid.setContent(pname);
            newGrids.add(grid);
            if (!son.isEmpty()) {
                for (int cid = 0; cid < son.size(); cid++) {
                    String cname = son.get(cid).getGname();
                    son.get(cid).setContent(pname + cname);
                    List<Grid> grandson = son.get(cid).getChildren();
                    newGrids.add(son.get(cid));
                    if (!grandson.isEmpty()) {
                        for (int rid = 0; rid < grandson.size(); rid++) {
                            String rname = grandson.get(rid).getGname();
                            grandson.get(rid).setContent(pname + cname + rname);
                            newGrids.add(grandson.get(rid));
                        }
                    }
                }
            }

            System.out.println(newGrids);
        }
        return newGrids;
    }

    public static List<Grid> fillGridRelation(List<Grid> grids) {
        List<Grid> gridRelation = new ArrayList<>();
        for (Grid grid : grids) {
            List<Grid> son = grid.getChildren();
            String pname = grid.getGname();
            grid.setContent(pname);

            if (!son.isEmpty()) {
                for (int cid = 0; cid < son.size(); cid++) {
                    String cname = son.get(cid).getGname();
                    son.get(cid).setContent(pname + cname);
                    List<Grid> grandson = son.get(cid).getChildren();
                    if (!grandson.isEmpty()) {
                        for (int rid = 0; rid < grandson.size(); rid++) {
                            String rname = grandson.get(rid).getGname();
                            grandson.get(rid).setContent(pname + cname + rname);
                            gridRelation.add(grandson.get(rid));
                        }
                    }else {
                        gridRelation.add(son.get(cid));
                    }
                }
            }else {
                gridRelation.add(grid);
            }
        }
        return gridRelation;
    }

}
